using System.Collections;
using System.Collections.Generic;
using UnityEngine;

namespace Adventure
{
    public static class FindExtensions
    {
        public static Transform FindTransform(this Transform parentTransform, string findName)
        {
            if (parentTransform == null || string.IsNullOrEmpty(findName))
            {
                return null;
            }

            if (parentTransform.name.Equals(findName))
            {
                return parentTransform;
            }

            for (int childIndex = 0; childIndex < parentTransform.childCount; ++childIndex)
            {
                Transform childTrans = parentTransform.GetChild(childIndex);

                if (childTrans.name.Equals(findName))
                {
                    return childTrans;
                }
                else
                {
                    Transform findTransform = FindTransform(childTrans, findName);
                    if (findTransform != null)
                    {
                        return findTransform;
                    }
                }
            }

            return null;
        }

        public static T FindTransform<T>(this Transform parentTransform, string findName) where T : UnityEngine.Component
        {
            if (parentTransform == null || string.IsNullOrEmpty(findName))
            {
                return null;
            }

            if (parentTransform.name.Equals(findName))
            {
                T t = parentTransform.GetComponent<T>();
                if (t == null)
                {
                    t = parentTransform.gameObject.AddComponent<T>();
                }

                return t;
            }

            for (int childIndex = 0; childIndex < parentTransform.childCount; ++childIndex)
            {
                Transform childTrans = parentTransform.GetChild(childIndex);

                if (childTrans.name.Equals(findName))
                {
                    T t = childTrans.GetComponent<T>();
                    if (t == null)
                    {
                        t = childTrans.gameObject.AddComponent<T>();
                    }

                    return t;
                }
                else
                {
                    Transform findTransform = FindTransform(childTrans, findName);
                    if (findTransform != null)
                    {
                        T t = findTransform.GetComponent<T>();
                        if (t == null)
                        {
                            t = findTransform.gameObject.AddComponent<T>();
                        }

                        return t;
                    }
                }
            }

            return null;
        }

        public static GameObject FindGameObject(this GameObject parentGameObject, string findName)
        {
            if (parentGameObject == null || string.IsNullOrEmpty(findName))
            {
                return null;
            }

            Transform parentTransform = parentGameObject.GetComponent<Transform>();
            if (parentTransform == null)
            {
                return null;
            }

            Transform findTransform = FindTransform(parentTransform, findName);
            if (findTransform != null && findTransform.gameObject != null)
            {
                return findTransform.gameObject;
            }

            return null;
        }

        public static T FindGameObject<T>(this GameObject parentGameObject, string findName) where T : UnityEngine.Component
        {
            if (parentGameObject == null || string.IsNullOrEmpty(findName))
            {
                return null;
            }

            Transform parentTransform = parentGameObject.GetComponent<Transform>();
            if (parentTransform == null)
            {
                return null;
            }

            Transform findTransform = FindTransform(parentTransform, findName);
            if (findTransform != null && findTransform.gameObject != null)
            {
                T t = findTransform.gameObject.GetComponent<T>();
                if (t == null)
                {
                    t = findTransform.gameObject.AddComponent<T>();
                }

                return t;
            }

            return null;
        }
    }
}